<!-- 侧边栏 -->
<template>
  <div>
    <el-menu
      :collapse-transition="false"
      :class="[isCollapse ? 'el-menu--collapse' : 'el-menu-vertical-demo']"
      :collapse="isCollapse"
      :default-active="activeMenu"
    >
      <h3>管理</h3>
      <el-menu-item
        v-for="item in noChildren"
        :index="item.path"
        :key="item.path"
        @click="handleMenu(item)"
      >
        <el-icon>
          <component :is="item.icon" />
        </el-icon>
        <span>{{ item.label }}</span>
      </el-menu-item>

      <el-sub-menu
        v-for="item in hasChildren"
        :index="item.path"
        :key="item.path"
      >
        <template #title>
          <el-icon>
            <component :is="item.icon" />
          </el-icon>
          <span>{{ item.label }}</span>
        </template>

        <el-menu-item-group>
          <el-menu-item
            v-for="subItem in item.children"
            :index="subItem.path"
            :key="subItem.path"
          >
            <el-icon>
              <component :is="subItem.icon" />
            </el-icon>
            <span>{{ subItem.label }}</span>
          </el-menu-item>
        </el-menu-item-group>
      </el-sub-menu>
    </el-menu>
  </div>
</template>

<script setup lang="ts">
import { computed, ref } from "vue";
import { useAllDataStore } from "@/stores";
import { useRoute, useRouter } from "vue-router";
type MenuItem = {
  path: string;
  label: string;
  name: string;
  icon: string;
  children?: MenuItem[];
};
const list = computed(() => {
  let temp : MenuItem[] = [
    {
      path: "/home",
      label: "首页",
      name: "home",
      icon: "house",
    },
  ];
  temp.push(...store.state.menuList);
  return temp;
});

const noChildren = computed(() => list.value.filter((item) => !item.children));
const hasChildren = computed(() => list.value.filter((item) => item.children));

const store = useAllDataStore();
const isCollapse = computed(() => store.state.isCollapse);

const route = useRoute();
const router = useRouter();
const activeMenu = computed(() => route.path);
const handleMenu = (item: any) => {
  router.push(item.path);
  store.selectTag(item);
  // console.log(store.state.tags)
};
</script>

<style scoped lang="less">
.el-menu {
  border-right-style: solid;
  border-width: 0.1mm;
  height: 100%;
  background-color: fade(darkgray, 20%);
  h3 {
    line-height: 50px;
    text-align: center;
  }
}
/* 未折叠时的样式 */
.el-menu-vertical-demo:not(.el-menu--collapse) {
  width: 130px;
  height: 100vh;
  animation: show 0.2s linear !important;
}

/*  折叠时样式 */
.el-menu--collapse {
  width: 60px;
  height: 100vh;
  animation: hide 0.2s linear !important;
}

/* 动画*/
@keyframes hide {
  from {
    width: 130px;
  }
  to {
    width: 60px;
  }
}
@keyframes show {
  from {
    width: 60px;
  }
  to {
    width: 130px;
  }
}
</style>
